package org.yujuan.springbootlearning.spring;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @author limengmeng
 */
@Configuration
public class DataSourceConfiguration {

    @Autowired
    private HikariProperties hikariProperties;

    @Bean
    public DataSource datasource(DataSourceProperties properties) throws NumberFormatException {
        HikariDataSource ds = new HikariDataSource();
        ds.setDriverClassName(properties.getDriverClassName());
        ds.setJdbcUrl(properties.getUrl());
        ds.setUsername(properties.getUsername());
        ds.setPassword(properties.getPassword());
        ds.setReadOnly(hikariProperties.isReadOnly());
        ds.setConnectionTimeout(hikariProperties.getConnectionTimeout());
        ds.setIdleTimeout(hikariProperties.getIdleTimeout());
        ds.setMaxLifetime(hikariProperties.getMaxLifetime());
        ds.setMaximumPoolSize(hikariProperties.getMaximumPoolSize());
        ds.setMinimumIdle(hikariProperties.getMinimumIdle());
        ds.setConnectionTestQuery(hikariProperties.getConnectionTestQuery());
        return ds;
    }

}
